(function (_super) {
    var EVENT_CLICK = Laya.Event.CLICK;

    function Sell(data) {
        Sell.super(this);

        this.itemName = null;
        this.itemId = null;
        this.maxSellNum = 0;
        this.unitPrice = 0;
        this.sellNum = 0;

        this.init(data);
    }
    Laya.class(Sell, "Alert.Sell", _super);
    var _proto = Sell.prototype;
    _proto.destroy = function () {
        _super.prototype.destroy.call(this, true);

        this.itemName = null;
        this.itemId = null;
        this.maxSellNum = null;
        this.unitPrice = null;
        this.sellNum = null;
    }

    _proto.init = function (data) {
        this.bindEvent();
        this.update(data);
    }
    _proto.bindEvent = function () {
        this.btnClose.on(EVENT_CLICK, this, this.destroy);
        this.cancelBtn.on(EVENT_CLICK, this, this.destroy);
        this.reductionBtn.on(EVENT_CLICK, this, this.minus);
        this.addBtn.on(EVENT_CLICK, this, this.plus);
        this.maxBtn.on(EVENT_CLICK, this, this.max);
        this.confirmBtn.on(EVENT_CLICK, this, this.sell);
    }
    _proto.update = function (data) {
        this.goodsName.text = data.name;
        this.picall.skin = PROPS_PATH + data.item_id + ".png";
        this.goodsNum.text = data.count;
        this.goodsPrice.text = data.sell;
        this.content.text = data.desc;

        this.itemName = data.name;
        this.itemId = data.item_id;
        this.maxSellNum = data.count;
        this.unitPrice = data.sell;
        this.sellNum = data.sellNum;

        this.calc();
    }
    _proto.minus = function () {
        this.sellNum--;
        if(this.sellNum <= 1){
            this.sellNum = 1;
        }
        this.calc();
    }
    _proto.plus = function () {
        this.sellNum++;
        if(this.sellNum >= this.maxSellNum){
            this.sellNum = this.maxSellNum;
        }
        this.calc();
    }
    _proto.max = function () {
        this.sellNum = this.maxSellNum;
        this.calc();
    }
    _proto.calc = function () {
        this.chooseNum.text = this.sellNum + "/" + this.maxSellNum;
        this.obtain.text = this.sellNum * this.unitPrice;
    }
    _proto.sell = function () {
        var config = {
            msg : '您将出售' + this.sellNum + '个' + this.itemName + '获得' + this.obtain.text + '金币',
            btn : {
                btnCancel : {
                    skin : "cancel"
                },
                btnConfirm : {
                    skin : "confirm",
                    click : function (count, itemId) {
                        ppt.io.emit({"cmd" : "bag::sell", params : {'count' : count, 'itemId' : itemId}});
                    }.bind(null, this.sellNum, this.itemId)
                }
            },
            close : true
        };
        ppt.director.popScene(new Alert.Public(config));
    }
})(ui.Alert.BagLayerUI);